# = test.forum.13.rb - Forum , Register and Perform Forum Post
# 
# See LICENSE.txt for licensing details.
#
# == Description
# Here we test the functionality for Registering as a new user and also test to  perform forum post.
#
require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'
    
	class TC_RegisterAndPerformForumPost_test < Test::Unit::TestCase
    
    def startup
      $UserName = randomChar(7)
      $Password = randomChar(10)
      $Email = randomChar(5) + '@' + randomChar(3) + '.com'
      
      $PostReplyBody = randomChar(20)
      $PostReplyTag = randomChar(5)
      $PostDeleteReason = randomChar(15)
      
      @@ie = Watir::IE.start($SITE_URL)
	  end
	
	  def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
	  end
    
    def test_01_verifyHomeText
      assert(ie.contains_text(/Community Server/))	
	  end
  
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToRegistrationPage
      ie.link(:text, 'Join').click
      assert(ie.contains_text("Sign in name"))      
      assert(ie.text_field(:id, /TitleBarSearchText$/).exists?)
    end
    
    def test_04_populateRegistrationForm      
      assert(ie.text_field(:id, /Username$/).exists?)
      ie.text_field(:id, /Username$/).set($UserName)
      assert(ie.text_field(:id, /Username$/).verify_contains($UserName))
      
      assert(ie.text_field(:id, /Password$/).exists?)
      ie.text_field(:id, /Password$/).set($Password)
      assert(ie.text_field(:id, /Password$/).verify_contains($Password))
      
      assert(ie.text_field(:id, /Password2$/).exists?)
      ie.text_field(:id, /Password2$/).set($Password)
      assert(ie.text_field(:id, /Password2$/).verify_contains($Password))
          
      assert(ie.text_field(:id, /Email$/).exists?)
      ie.text_field(:id, /Email$/).set($Email)
      assert(ie.text_field(:id, /Email$/).verify_contains($Email))
      
      assert(ie.text_field(:id, /Email2$/).exists?)
      ie.text_field(:id, /Email2$/).set($Email)
      assert(ie.text_field(:id, /Email2$/).verify_contains($Email))      
    end
	  
	  def test_05_submitRegistrationForm      
      assert(ie.link(:id, /CreateAccount$/).exists?)
      ie.link(:id, /CreateAccount$/).click
	  end

    def test_07_verifyLogoutLinkExists
      assert_equal $SITE_URL + 'logout.aspx', ie.link(:text, 'Sign out').href
      #assert(ie.contains_text("Signed in as"))
    end
    
    def test_08_navigateToForumPage
      assert(ie.link(:text, 'Forums').exists?)
      ie.link(:text, 'Forums').click
    end
    
    def test_09_verifyLinksOnForumPage

      assert(ie.link(:text, 'Help').exists?)
      assert(ie.link(:text, 'Inbox').exists?)
      assert(ie.link(:text, 'Home').exists?)
      assert(ie.link(:text, 'Blogs').exists?)
      assert(ie.link(:text, 'Forums').exists?)
      assert(ie.link(:text, 'Photos').exists?)
      assert(ie.link(:text, 'Downloads').exists?)
     
      assert(ie.link(:text, 'Sample Forums').exists?)
      assert(ie.link(:id, /ToggleLink$/).exists?)
      assert(ie.link(:text, 'Sample Forum').exists?)
      #assert(ie.link(:text, 'Sample Forum Post').exists?)
      assert(ie.link(:text, 'Who is Online').exists?)

      assert(ie.link(:text, 'View all users').exists?)
      assert(ie.link(:text, 'Posts you have not read').exists?)
      assert(ie.link(:text, 'Browse All Tags').exists?)
    end
    
    def test_10_navigateToSamplePost
      assert(ie.link(:text, 'New Post').exists?)
      ie.link(:text, 'New Post').click      
    end
    
    def test_11_replyToSamplePostLinkTest
      assert(ie.link(:text, 'Reply').exists?)
      ie.link(:text, 'Reply').click
      
      assert(ie.contains_text("Reply to an Existing Message"))
      assert(ie.text_field(:id, /PostSubject$/).exists?)
      assert(ie.text_field(:id, /PostSubject$/).verify_contains("Re: New Post"))
    end
    
     def test_12_testTextFieldsExistAndEnabled
      assert(ie.text_field(:id, /PostSubject$/).exists?)
      assert(ie.text_field(:id, /Editor$/).exists?)
      assert(ie.text_field(:id, /Tags$/).exists?)
      
      assert(ie.text_field(:id, /PostSubject$/).enabled?)
      assert(ie.text_field(:id, /Editor$/).enabled?)
      assert(ie.text_field(:id, /Tags$/).enabled?)
    end
    
    def test_13_enterReplyAndSubmit
      ie.ie.Document.parentWindow.execScript("tinyMCE.getInstanceById(tinyMCE.getEditorId('mce_editor_0')).execCommand('mceSetContent', false, '" + $PostReplyBody + "')")
      ie.text_field(:id, /Tags$/).set($PostReplyTag)
      
      assert(ie.button(:id, /PostButton$/).exists?)
      ie.button(:id, /PostButton$/).click
    end
    
    def test_14_checkReplyExists
      assert(ie.contains_text('New Post'))      
    end
    
  end